import jsonpath

# 安装jsonpath  pip install jsonpath


# 示例 JSON 数据
data = {
    "store": {
        "book": [
            {"title": "超越猿类：人类道德心理进化史", "price": 47},
            {"title": "数学思维的力量", "price": 42},
            {"title": "人工智能时代与人类价值", "price": 63},
            {"title": "三体全集 : 地球往事三部曲", "price": 168},
            {"title": "千与千寻", "price": 78},
            {"title": "秦汉史讲义", "price": 138},
            {"title": "活着", "price": 28},
            {"title": "哈利·波特", "price": 498}
        ],
        "automobile": {
            "automarke":"XIAOMI SU7 Ultra",
            "color": "海湾蓝",
            "price": 529900
        }
    }
}


# 查询所有书籍的标题
titles = jsonpath.jsonpath(data, "$.store.book[*].title")
print("所有书籍的标题:", titles)  
# 输出:  ['超越猿类：人类道德心理进化史', '数学思维的力量', '人工智能时代与人类价值', '三体全集 : 地球往事三部曲', '千与千寻', '秦汉史讲义', '活着', '哈利·波特']

# 查询第一本书
first_book = jsonpath.jsonpath(data, "$.store.book[0]")
print("第一本书:", first_book)  
# 输出:  [{'title': '超越猿类：人类道德心理进化史', 'price': 47}]

# 查询价格大于 20 的书籍
expensive_books = jsonpath.jsonpath(data, "$.store.book[?(@.price > 50)]")
print("价格大于 50 的书籍:", expensive_books)  
# 输出:  [{'title': '人工智能时代与人类价值', 'price': 63}, {'title': '三体全集 : 地球往事三部曲', 'price': 168}, {'title': '千与千寻', 'price': 78}, {'title': '秦汉史讲义', 'price': 138}, {'title': '哈利·波特', 'price': 498}]

# 查询汽车品牌
automobile_mark = jsonpath.jsonpath(data, "$.store.automobile.automarke")
print("汽车品牌:", automobile_mark)  
# 输出:  ['XIAOMI SU7 Ultra']

# 查询汽车颜色
automobile_color = jsonpath.jsonpath(data, "$.store.automobile.color")
print("汽车颜色:", automobile_color)  
# 输出:  ['海湾蓝']

# 递归查询所有价格
all_prices = jsonpath.jsonpath(data, "$..price")
print("所有价格:", all_prices)  
# 输出: [47, 42, 63, 168, 78, 138, 28, 498, 529900]